<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../../assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <el-menu
        class="el-menu-top"
        mode="horizontal"
        background-color="#23262E"
        text-color="#fff"
        active-text-color="#409EFF"
      >
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <img v-if="!userInfo.user_pic" src="../../assets/logo.png" class="avatar" alt="" />
          <img v-else  :src="userInfo.user_pic" class="avatar" alt="" />
            <span>个人中心</span>
          </template>
          <el-menu-item index="1-1"
            ><i class="el-icon-s-operation"></i>基本资料</el-menu-item
          >
          <el-menu-item index="1-2"
            ><i class="el-icon-camera"></i>更换头像</el-menu-item
          >
          <el-menu-item index="1-3"
            ><i class="el-icon-key"></i>重置密码</el-menu-item
          >
        </el-submenu>
        <el-menu-item index="2" @click="logoutHandler"
          ><i class="el-icon-switch-button"></i>退出</el-menu-item
        >
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <!-- 左侧边栏区域 -->
      <el-aside width="200px">
        <div class="user-box">
          <img v-if="!userInfo.user_pic" src="../../assets/logo.png" alt="" />
          <img v-else  :src="userInfo.user_pic" alt="" />
          <span>欢迎 {{ userInfo.nickname || userInfo.username }}</span>
        </div>
      <!-- 菜单 -->
       <el-menu
      :default-active="$router.path"
      class="el-menu-vertical-demo"
      background-color="#23262E"
      text-color="#fff"
      active-text-color="#409EFF">
      <template v-for="(item,index) in menuList">
       <el-menu-item v-if="!item.children" :key="index" @click="$router.push(item.indexPath)" :index="item.indexPath"> <i :class="item.icon"></i> {{item.title}}</el-menu-item>
         <el-submenu v-else :key="index" :index="item.indexPath">
           <template  slot="title"> <i :class="item.icon"></i> {{item.title}}</template>
            <el-menu-item v-for="(ele,index) in item.children" :key="index"  @click="$router.push(ele.indexPath)" :index="ele.indexPath"> <i :class="ele.icon"></i> {{ele.title}}</el-menu-item>
         </el-submenu>
         </template>
       </el-menu>
      </el-aside>

      <el-container>
        <!-- 页面主体区域 -->
        <router-view></router-view>
        <!-- 底部 footer 区域 -->
        <el-footer>© www.itheima.com - 黑马程序员</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'Main',
  data () {
    return {
      menuList: []
    }
  },
  methods: {
    logoutHandler () {
      this.$confirm('确认退出吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '已退出登录'
          })
          this.$store.commit('user/getToken', '')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    }
  },
  async mounted () {
    this.$store.dispatch('user/getUserInfo')
    const { data: res } = await this.$http.get('/my/menus')
    // console.log(res)
    if (res.code !== 0) return this.$message.error(res.message)
    this.$message.success(res.message)
    this.menuList = res.data
  },
  computed: {
    ...mapState('user', ['userInfo'])
  }
}
</script>

<style lang="less" scoped>
.el-container{
  justify-content: space-between;

}
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
// 左侧边栏用户信息区域
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}
</style>
